package com.ym.b2b.payment.util;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;

/**
 * @ClassName: PayUtil
 * @Description: TODO
 * @Author tangxj
 * @Date 2015-4-1 下午2:33:44
 * @Modify
 * @CopyRight B2B小组
 */

public class PayUtil {

    public static String doHttpPost(String serverUrl, NameValuePair[] data) throws Exception {
        String body = "";
        System.out.println(serverUrl);
        HttpClient httpClient = new HttpClient();
        PostMethod postMethod = new PostMethod(serverUrl);
        postMethod.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=gbk");

        postMethod.setRequestBody(data);
        int statusCode = httpClient.executeMethod(postMethod);

        if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
            Header locationHeader = postMethod.getResponseHeader("location");
            String location = null;
            if (locationHeader != null) {
                location = locationHeader.getValue();
                System.out.println("The page was redirected to:" + location);
            } else {
                System.err.println("Location field value is null.");
            }
        } else {
            body = postMethod.getResponseBodyAsString();

        }
        postMethod.releaseConnection();

        return body;
    }

}
